// import bullet from './bullet'
import Circle from './Circle'
import keyBorad from './keyBorad'
import { Collision } from './Collision'
import { bullet } from './bullet'
import { Engine } from './engine'



/**
 * 玩家控制对象
 *
 * @class palyer
 */
class palyer {

    constructor() {
        /**
         * 验证判断对象
         * @memberof palyer
         */
        this.obj = new Circle(20);

        this.bindMove();
        /**
         * 移动速度值
         * @memberof palyer
         */
        this.speed = 6;
        /**
         * 移动事件绑定对象
         * @memberof palyer
         */
        this.moveEvent = {
            X: function () { },
            Y: function () { },
        }
        /**
         * 生命值对象
         * @memberof palyer
         */
        this.Health = document.createElement('div');
        this.Health.classList = ['Health'];
        this.HPele = document.createElement('div');
        this.HPele.classList = ['HP'];
        this.Health.appendChild(this.HPele);
        /**
         * 生命值
         * @memberof palyer
         */
        this.hp = 10;
        this.HPele.style.width = this.hp * 10 + '%'

        /**
         * 是否死亡
         * @memberof palyer
         */
        this.isOver = false;
        document.body.appendChild(this.Health);

        /**
         * 帧率数值（用于计算子弹算法）
         * @memberof palyer
         */
        this.index = 0;
        /**
         * 渲染引擎对象
         * @memberof palyer
         */
        this.DrawEvent = Engine;

        // 初始化玩家位置
        this.obj.x = this.DrawEvent.realWidth / 2;
        this.obj.y = this.DrawEvent.realHeight - 50;
    }

    /**
     * 玩家与子弹相对位置计算
     * 被击中事件计算
     * @memberof palyer
     */
    checkHit() {
        var color = '#ffffff';
        for (let i = 0; i < this.DrawEvent.enemyBullets.length; i++) {
            const element = this.DrawEvent.enemyBullets[i];
            let ans = Collision(element, this.obj);
            if (ans) {
                this.DrawEvent.removeObject(element, 'enemyBullets');
                this.hp--;
                this.HPele.style.width = this.hp * 10 + '%';
                // if (this.hp <= 0 && !this.isOver) {
                //     this.isOver = true;
                //     alert('游戏结束');
                // }
                color = '#FF0000';
            }
        }
        this.obj.color = color;
    }

    /**
     * 渲染算法
     *
     * @param {*} c
     * @memberof palyer
     */
    draw(c) {
        c.translate(this.obj.x, this.obj.y);
        c.beginPath();
        c.arc(0, 0, this.obj.r, 0, Math.PI * 2);
        c.globalAlpha = 1;
        c.strokeStyle = this.obj.color;
        c.lineWidth = 2;
        c.stroke();
        c.restore();

        // this.obj.draw(c)
        if (!this.isOver) {
            this.moveEvent.X();
            this.moveEvent.Y();
            this.index++;
            if (this.index % 4 == 0) {
                var _bullet = new bullet(3);
                _bullet.x = this.obj.x - 10;
                _bullet.y = this.obj.y - 20;
                _bullet.direction = -10;
                _bullet.hit = 1;
                this.DrawEvent.addObject(_bullet, 'playerBullets');
                var _bullet = new bullet(3);
                _bullet.x = this.obj.x + 10;
                _bullet.y = this.obj.y - 20;
                _bullet.direction = -10;
                _bullet.hit = 1;
                this.DrawEvent.addObject(_bullet, 'playerBullets');
            }
            this.checkHit()
        }
    }

    /**
     * 移动事件
     *
     * @param {Number} x X轴移动方向
     * @param {Number} y Y轴移动方向
     * @return {Function} 移动绑定方法
     * @memberof palyer
     */
    _moveEvent(x, y) {
        if (x != 0) {
            return () => {
                this.obj.x += x * this.speed;
                if (this.obj.x < 0) {
                    this.obj.x = 0
                } else if (this.obj.x > this.DrawEvent.realWidth) {
                    this.obj.x = this.DrawEvent.realWidth
                }
            }
        } else if (y != 0) {
            return () => {
                this.obj.y += y * this.speed;
                if (this.obj.y < 0) {
                    this.obj.y = 0
                } else if (this.obj.y > this.DrawEvent.realHeight) {
                    this.obj.y = this.DrawEvent.realHeight
                }
            }
        }
    }

    /**
     * 按键绑定事件
     *
     * @memberof palyer
     */
    bindMove() {
        keyBorad.on('up', () => {
            this.moveEvent.Y = this._moveEvent(0, -1);
        }, () => {
            var down = keyBorad.getKeyPerss(['down'])
            if (down) {
                this.moveEvent.Y = this._moveEvent(0, 1);
            } else {
                this.moveEvent.Y = () => { }
            }
        })
        keyBorad.on('down', () => {
            this.moveEvent.Y = this._moveEvent(0, 1);
        }, () => {
            var up = keyBorad.getKeyPerss(['up'])
            if (up) {
                this.moveEvent.Y = this._moveEvent(0, -1);
            } else {
                this.moveEvent.Y = () => { }
            }
        })
        keyBorad.on('left', () => {
            this.moveEvent.X = this._moveEvent(-1, 0);
        }, () => {
            var right = keyBorad.getKeyPerss(['right']);
            if (right) {
                this.moveEvent.X = this._moveEvent(1, 0);
            } else {
                this.moveEvent.X = () => { }
            }
        })
        keyBorad.on('right', () => {
            this.moveEvent.X = this._moveEvent(1, 0);
        }, () => {
            var left = keyBorad.getKeyPerss(['left']);
            if (left) {
                this.moveEvent.X = this._moveEvent(-1, 0);
            } else {
                this.moveEvent.X = () => { }
            }
        })
    }
}



export default palyer